<?php
namespace Home\Model;
use Think\Model;
class TRoleModel extends Model
{
	private $role_id  = -1;

	public function setRoleId($role_id){
		$this->role_id = $role_id;
		return $this;
	}

	public function getRolerTreeByFields($fields=null){
		list($fields,$useKey) = isIndex($fields);
		$role_tree = $this->getRolersTree();
		return $fields?getTreeFields($role_tree,$fields,$useKey):$role_tree;
	}

	public function getRolerArrayByFields($fields=null){
		list($fields,$useKey) = isIndex($fields);
		$role_array = $this->getRolersArray();
		return $fields?getArrayFields($role_array,$fields,$useKey):$role_array;
	}

	private function getRolersTree()
	{
		// 仅保存一次
		$role_tree = session('role_tree');
		if(!$role_tree){
			$role_tree = $this->select();
			$role_tree = getChildTree($role_tree,$this->role_id,'id','parent_id');
			session('role_tree',$role_tree);
		}
		return $role_tree;
	}

	private function getRolersArray(){
		// 仅保存一次
		$role_array = session('role_array');
		if(!$role_array){
			$role_tree = $this->getRolersTree($this->role_id);
			$role_array = treeToArray($role_tree);
			session('role_array',$role_array);
		}
		return $role_array;
	}
}